<template>
  <div class="goods-list">
    <router-link
      tag="div"
      :to="`/home/goodsinfo/${v.id}`"
      class="goods-item"
      v-for="v in goodsList"
      :key="v.id"
    >
      <img src="../../images/leimu05的副本.jpeg" alt="" />
      <p class="title" v-text="v.title">蕾姆是蓝色的</p>
      <div class="dec">
        <p class="price">
          <span class="new">¥{{ v.sell_price }}</span
          ><span class="old">¥{{ v.market_price }}</span>
        </p>
        <p class="sell">
          <span>热卖中</span><span>剩余{{ v.stock_quantity }}件</span>
        </p>
      </div>
    </router-link>
    <!-- 加载更多 -->
    <mt-button type="danger" size="large" @click="loadMore">加载更多</mt-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      goodsList: [],
      page: 1,
      // 用来判读数据是否加载完毕的节流阀
      isLoaded: false,
    }
  },
  methods: {
    // 一、获取商品列表
    async getGoodsList() {
      // 1.1 发送请求
      const { data } = await this.$http.get(`/api/getgoods?pageindex=${this.page}`)
      if (data.status === 0) {
        // 2.1 判断请求的数据是否为空,为空就将节流阀改成true,表示加载完毕
        if (data.message.length === 0) {
          this.isLoaded = true
        }
        // console.log(data)
        // 1.2 保存获取到的数据,根据数据渲染页面
        this.goodsList = this.goodsList.concat(data.message)
      }
    },

    // 二、点击加载更多按钮
    loadMore() {
      // 2.2 判断数据是否加载完毕,false表示没加载完
      if (this.isLoaded == false) {
        // console.log(1)
        // 2.3 页码+1
        this.page++
        // 2.4 重新发请求获取下一页的数据
        this.getGoodsList()
      }
    },
  },
  created() {
    this.getGoodsList()
  },
}
</script>

<style lang="scss" scoped>
.goods-list {
  padding: 7px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  .goods-item {
    width: 49%;
    padding: 4px;
    box-shadow: 0 0 10px #ccc;
    margin-bottom: 7px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .dec {
      background-color: #ddd;
      padding: 5px;
      p {
        margin-bottom: 3px;
      }
      .new {
        color: red;
        font-size: 15px;
        margin-right: 10px;
      }
      .old {
        font-size: 12px;
        text-decoration: line-through;
      }
      .sell {
        display: flex;
        justify-content: space-between;
        font-size: 13px;
      }
    }
  }
  img {
    width: 100%;
    margin-bottom: 3px;
    vertical-align: middle;
  }
}
</style>
